﻿--[[
    @Brief    验证码逻辑
    @Author    james
    @Date    2017-05-23
--]]

EPictChkUI = 
{
    sChkView  = "pnlPictChk";
    sBtnPict  = "btnPict";
    sBtnRenew = "btnRenew";
    sEdtInput = "pnlEdtBG\\edtInput";
    sRtbTips  = "rtbTips";
    sBtnValid = "btnConfirm";
    sBtnBack  = "btnCancel";

};

TPictCheck = 
{
    InitView = function(nRootID)
        TPictCheck.nRootID = nRootID;
    end;

    UnInitView = function()
        TPictCheck.nRootID  = 0;
        TPictCheck.pChkView = nil;
        TPictCheck.pBtnPict = nil;
        TPictCheck.pRtbTips = nil;
        TPictCheck.pEdtCode = nil;
    end;

    ShowView = function()
        if TPictCheck.CheckUI() then
            TPictCheck.pChkView:Show(TRUE);
            --[[
            local     sTips = LoadStr("STR_LOGIN_PICTCHK_TIPS");
                            "需要验证码的原因可能是：<br>" .. 
                            "您本次登录地与常用登录地不符<br>"..
                            "您的网络有危及QQ用户安全的行为<br><br>" ..
                            "<f al=\"center\"><a id=\"1\">查看登陆地</a>  <a id=\"2\">设置登录保护</a></f>";
            ]]
            local     sTemp = U8toCUIStr(LoadStr("STR_LOGIN_PICTCHK_TIPS"));

            TPictCheck.pRtbTips:Clear();
            TPictCheck.pRtbTips:PushBackHtmlString(sTemp);

            TPictCheck.pEdtCode:SetFocus();
        end;
    end;

    HideView = function()
        if nil ~= TPictCheck.pChkView then
            TPictCheck.pChkView:Show(FALSE);
        end;
    end;

    -- 设置数据
    SetStatus = function(zStatus)
        if nil == zStatus then
            TGPLog.LogE("PictChk", "Set status, param is nil.");
            return;
        end;

        if nil == zStatus["pic_size"] or nil == zStatus["pic"] then
            TGPLog.LogI("PictChk", "Picture param is nil.");
            return;
        end;

        if nil ~= TPictCheck.pBtnPict then
            if TGPLog.bDebug then
                local     sPath  = base_utility.get_work_path() .. "\\tpf_ui\\res\\";
                local     sImage = "ONEIMAGE:" .. sPath .. zStatus["pic"] .. ";Stretch;0;";

                ui_utility.setproperty_by_id(TPictCheck.pBtnPict:GetControlID(),
                                             "Background", sImage);
            else
                local     sImage = "ONEIMAGE:" .. zStatus["pic"] .. ";Stretch;0;";
                ui_utility.setproperty_by_id(TPictCheck.pBtnPict:GetControlID(),
                                             "Background", sImage);
            end;
        end;

        TGPLog.LogI("PictChk", "Uin:" .. zStatus["uin"] .. 
                    "  ServiceID:".. zStatus["service_id"]);

        TPictCheck.nCurUin = zStatus["uin"];
        TPictCheck.nSvrID  = zStatus["service_id"];

        if nil ~= TPictCheck.pEdtCode then
            TPictCheck.pEdtCode:SetFocus();
        end;
    end;

    -- 检查界面
    CheckUI = function()
        if nil ~= TPictCheck.pChkView then
            return true;
        end;

        TPictCheck.pChkView = ui_utility.get_ctrl_ptr_by_path(
                                TPictCheck.nRootID, EPictChkUI.sChkView);

        if nil == TPictCheck.pChkView then
            TGPLog.LogE("PictChk", "Get check view fail.");
            return false;
        end;

        local     nTempID = TPictCheck.pChkView:GetControlID();

        -- 验证码图片
        TPictCheck.pBtnPict = ui_utility.get_ctrl_ptr_by_path(nTempID,EPictChkUI.sBtnPict);
        if nil == TPictCheck.pBtnPict then
            TGPLog.LogE("PictChk", "Get check view fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(TPictCheck.pBtnPict:GetControlID(), 
                                     "OnClick", "TPictCheck.OnClkRenew");

        -- 输入框
        TPictCheck.pEdtCode = ui_utility.get_ctrl_ptr_by_path(nTempID,EPictChkUI.sEdtInput);
        if nil == TPictCheck.pEdtCode then
            TGPLog.LogE("PictChk", "Get code input edit fail.");
            return false;
        end;

        -- 提示
        TPictCheck.pRtbTips = ui_utility.get_ctrl_ptr_by_path(nTempID,EPictChkUI.sRtbTips);
        if nil == TPictCheck.pRtbTips then
            TGPLog.LogE("PictChk", "Get tips fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(TPictCheck.pRtbTips:GetControlID(),
                                       "OnRTBClickHyperLink", "TPictCheck.OnClkLink");

        -- 刷新按钮
        local     nBtnID = ui_utility.get_ctrl_id_by_path(nTempID, EPictChkUI.sBtnRenew);

        if nil == nBtnID or 0 == nBtnID then
            TGPLog.LogE("PictChk", "Get renew button fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(nBtnID, "OnClick", "TPictCheck.OnClkRenew");

        -- 确定按钮
        nBtnID = ui_utility.get_ctrl_id_by_path(nTempID,EPictChkUI.sBtnValid);
        if nil == nBtnID or 0 == nBtnID then
            TGPLog.LogE("PictChk", "Get valid button fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(nBtnID, "OnClick", "TPictCheck.OnClkValid");

        -- 取消按钮
        nBtnID = ui_utility.get_ctrl_id_by_path(nTempID,EPictChkUI.sBtnBack);
        if nil == nBtnID or 0 == nBtnID then
            TGPLog.LogE("PictChk", "Get cancel button fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(nBtnID, "OnClick", "TPictCheck.OnClkCancel");

        return true;
    end;

    -- 刷新验证码
    OnClkRenew = function(pUILib, nBtnID)
        local     zParam = {};

        zParam["service_id"] = TPictCheck.nSvrID;
        comm_center.call_service("Svr_RenewLoginPict", zParam);
    end;

    -- 验证
    OnClkValid = function(pUILib, nBtnID)
        if nil == TPictCheck.pEdtCode then
            TGPLog.LogE("PictChk", "Valid picture, edit code is null.");
            return;
        end;

        local     sCode = TPictCheck.pEdtCode:GetText();

        if nil == sCode or "" == sCode then
            TPictCheck.pEdtCode:SetFocus();
            return;
        end;

        local     sTemp  = CUIStrtoU8(sCode);
        local     zParam = {};

        zParam["uin"]         = TPictCheck.nCurUin;
        zParam["service_id"] = TPictCheck.nSvrID;
        zParam["code"]          = sTemp;
        comm_center.call_service("Svr_ValidPictCode", zParam);
    end;

    -- 取消
    OnClkCancel = function(pUILib, nBtnID)
        local     zParam = {};

        zParam["uin"]         = TPictCheck.nCurUin;
        zParam["service_id"] = TPictCheck.nSvrID;
        comm_center.call_service("Svr_CancelLogin", zParam);
    end;

    -- 点击链接
    OnClkLink = function(pUILib, nCtrlID, rtObjt, nLink, sText, nX, nY, 
                         cKey, bCtrl, bShift, bAlt, sLink)
        if 1 == nLink then    -- 查看登录地
            web_utility.open_browser(LoadStr("URL_LOGIN_VIEW_MYIP"), false, true);        -- "http://ip.qq.com/cgi-bin/myip"
        else                -- 设置登录保护
            web_utility.open_browser(LoadStr("URL_LOGIN_MYLOGIN_PROT"), false, true);     --  "http://aq.qq.com/cn2/safe_service/my_login_prot?source_id=2192"
        end;
    end;

    -- 界面控件
    nRootID  = 0;
    pChkView = nil;
    pBtnPict = nil;
    pRtbTips = nil;
    pEdtCode = nil;

    -- 状态
    nSvrID   = 0;
    nCurUin  = 0;

};